//预加载
function MyToast(el,obj){
    this.content=obj.content||'数据加载中';
    this.el=$(el);
    this.el.hide();
    this.show=function(){
        this.el.show();
    };
    this.hide=function(){
        this.el.hide();
    };
    var maskStyle={
        position: 'fixed',
        zIndex: 1000,
        top: 0,
        right: 0,
        left: 0,
        bottom: 0
    };
    var toastStyle={
        position: 'fixed',
        zIndex: 5000,
        width: '7.6em',
        minHeight: '7.6em',
        top: '180px',
        left: '50%',
        marginLeft: '-3.8em',
        background: 'rgba(17, 17, 17, 0.7)',
        textAlign: 'center',
        borderRadius: '5px',
        color: '#FFFFFF'
    };
    var loadingStyle={
        margin: '30px 0 0',
        width: '38px',
        height: '38px',
        verticalAlign: 'baseline',
        display: 'inline-block',
        animation: 'Loading 1s steps(12, end) infinite',
        background:'url() no-repeat',
        backgroundSize: '100%'
    };
    var contentStyle={
        margin: '0 0 15px'
    };
    var html='<div class="toastMask"></div>'
    html+='<div class="toastBox">'
    html+=  '<i class="toastLoading"></i>'
    html+=  '<p class="toastContent">'+this.content+'</p>'
    html+='</div>'
    this.el.append(html);
    $('.toastMask').css(maskStyle);
    $('.toastBox').css(toastStyle);
    $('.toastLoading').css(loadingStyle);
    $('.toastContent').css(contentStyle);
}
(function($){
    //对话框
    $.Dialog =function(options){
        var defaults={
            type:'toast',//alert;confirm;prompt
            content:'欢迎使用',//弹框内容
            textColor:'#FFFFFF',//字体颜色
            backgroundColor:'rgba(0,0,0,.6)',//背景框背景色
            sureColor:'#000',//确定按钮的字体颜色
            confirm2Btn1Txt:'操作1',
            confirm2Btn2Txt:'操作2',
            callBack:$.noop
        };
        var option = $.extend(defaults, options);
        var type=option.type;
        var content=option.content;
        var textColor=option.textColor;
        var backgroundColor=option.backgroundColor;
        var sureColor=option.sureColor;
        var callBack=option.callBack;
        var confirm2Btn1Txt=option.confirm2Btn1Txt;
        var confirm2Btn2Txt=option.confirm2Btn2Txt;
        var activeBtnColor=option.activeBtnColor;
        if(type==='toast'){
            var style1={
                position: 'fixed',
                top: '50%',
                left: '50%',
                fontSize:'1rem',
                transform: 'translate(-50%,-50%)',
                padding: '10px 20px',
                borderRadius:'10px',
                backgroundColor:backgroundColor,
                color: textColor,
                zIndex:10000
            };
            var html='<div class="toast">'+content+'</div>';
            $('body').append(html);
            $('.toast').css(style1);
            var timer=setTimeout(function(){
                $('.toast').fadeOut(200);
                clearTimeout(timer);
            },2000);
        }else if(type==='alert'){
            var alertStyle={
                position: 'fixed',
                top:'0',
                left: '0',
                bottom: '0',
                right:'0',
                backgroundColor:backgroundColor,
                zIndex: '10000'
            };
            var alertBoxStyle={
                position: 'absolute',
                top:'50%',
                left:'50%',
                transform:'translate(-50%,-50%)',
                width: '70%',
                maxWidth: '300px',
                backgroundColor: '#FFF',
                textAlign: 'center',
                borderRadius: '10px',
                overflow: 'hidden',
            };
            var alertTipStyle={
                minHeight: '40px',
                fontSize: '15px',
                lineHeight: '1.3',
                wordWrap: 'break-word',
                wordBreak: 'break-all',
                padding: '2.7em 20px 1.7em',
                color:'#000',
            };
            var alertBtnStyle={
                lineHeight: '48px',
                fontSize: '16px',
                borderTop: '1px solid #ddd',
                color:sureColor
            };
            var html2='<div class="alert"><div class="alertBox">'
                html2+=     '<p class="alertTip">'+content+'</p>'
                html2+=     '<p class="alertBtn">知道了</p>'
                html2+='</div></div>';
            $('body').append(html2);
            $('.alert').css(alertStyle);
            $('.alertBox').css(alertBoxStyle);
            $('.alertTip').css(alertTipStyle);
            $('.alertBtn').css(alertBtnStyle).click(function(){
                $(this).css('backgroundColor','#ddd');
                $('.alert').hide();
                callBack();
            });
        }else if(type==='confirm'){
            var confirmStyle={
                position: 'fixed',
                top:'0',
                left: '0',
                bottom: '0',
                right:'0',
                backgroundColor:backgroundColor,
                zIndex: '10000'
            };
            var confirmBoxStyle={
                position: 'absolute',
                top:'50%',
                left:'50%',
                transform:'translate(-50%,-50%)',
                width: '80%',
                maxWidth: '300px',
                backgroundColor: '#FFF',
                textAlign: 'center',
                borderRadius: '10px',
                overflow: 'hidden',
            };
            var confirmTipStyle={
                minHeight: '40px',
                fontSize: '15px',
                lineHeight: '1.3',
                wordWrap: 'break-word',
                wordBreak: 'break-all',
                padding: '2.7em 20px 1.7em',
                color:textColor,
            };
            var confirmBtnStyle={
                lineHeight: '48px',
                float:'left',
                width:'50%',
                fontSize: '16px',
                borderTop: '1px solid #ddd',
                color:sureColor
            };
            var html3='<div class="confirm"><div class="confirmBox">'
            html3+=     '<p class="confirmTip">'+content+'</p>'
            html3+=     '<p class="confirmBtn sureBtn" style="border-right: 1px solid #ddd">确定</p><p class="confirmBtn cancelBtn">取消</p>'
            html3+='</div></div>';
            $('body').append(html3);
            $('.confirm').css(confirmStyle);
            $('.confirmBox').css(confirmBoxStyle);
            $('.confirmTip').css(confirmTipStyle);
            $('.confirmBtn').css(confirmBtnStyle)
            $('.cancelBtn').click(function(){
                $(this).css('backgroundColor','#ddd');
                $('.confirm').hide();
            });
            $('.sureBtn').click(function(){
                $(this).css('backgroundColor','#ddd');
                $('.confirm').hide();
                callBack();
            });
        }
    };
    //处理时间
    $.dealTime=function(time){
        var now=new Date();
        var nowTimes=now.getTime();
        var nowYear=now.getFullYear();
        var nowMonth=now.getMonth()+1;
        var nowDate=now.getDate();
        var nowHour=now.getHours();
        var nowMin=now.getMinutes();
        var nowSec=now.getSeconds();
        if(time){
            var  old=new Date(time);
            var  oldTimes= old.getTime();
            var  oldYear= old.getFullYear();
            var  oldMonth= old.getMonth()+1;
            var  oldDate= old.getDate();
            var  oldHour= old.getHours();
            var  oldMin= old.getMinutes();
            var  oldSec= old.getSeconds();
            if(nowTimes-oldTimes<=120000){
                return '刚刚';
            }else if(nowTimes-oldTimes>120000&&nowTimes-oldTimes<=1200000){
                return parseInt((nowTimes-oldTimes)/(60*1000))+'分钟前';
            }else if(nowTimes-oldTimes>120000&&nowYear===oldYear&&nowMonth===oldMonth&&nowDate===oldDate){
                return oldHour+':'+oldMin;
            }else if(nowYear===oldYear&&(nowMonth!==oldMonth||nowDate!==oldDate)){
                return oldMonth+'-'+oldDate;
            }else if(nowYear!==oldYear){
                return oldYear+'-'+oldMonth+'-'+oldDate;
            }
        }else {
            return nowYear+'-'+nowMonth+'-'+nowDate;
        }
    };
    //计算中间弹性高度
    $.flexHeight=function(options){
        var defaults={
            top:0,
            bottom:0
        };
        var flexHeight;
        var option = $.extend(defaults, options);
        var top=option.top;
        var bottom=option.bottom;
        var clientH=$(window).height();
        flexHeight=clientH-top-bottom;
        return flexHeight;
    };
    ///////////////////限制字数的方法
    $.limitWords=function(options){
        var defaults={
            content:'',
            number:220,
            needReturn:{returnWord:''}
        };
        var option = $.extend(defaults, options);
        var number=option.number;
        var content=option.content;
        var needReturn=option.needReturn.returnWord;
        if(content){
            var textLen=content.length;
            if(textLen>number){
                return content.slice(0,number)+"...";
            }
            return content;
        }else {
            if(needReturn!==''){
                return needReturn;
            }else {
                return ''
            }

        }
    };
    /////////根据关键词添加高亮
    $.highLight=function(options){
        var defaults={
            content:'',
            color:'red',
            keyWord:''
        };
        var option=$.extend(defaults,options);
        var color=option.color;
        var content=option.content;
        var keyWord=option.keyWord;
        if(content){
            if(keyWord&&color){
                if(content.indexOf(keyWord)!==-1){
                    var reg=new RegExp(keyWord,'g');
                    return content.replace(reg,"<span style='color:"+color+" '>"+keyWord+"</span>")
                }else{
                    return content
                }
            }else {
                return content
            }
        }else{
          return  '';
        }
    };
    //////////////获取地址栏参数
    $.getUrlParam=function(ky){
        var reg = new RegExp("(^|&)"+ ky +"=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        var r = window.location.search.substr(1).match(reg);  //匹配目标参数
        if (r!=null) return decodeURI(r[2]); return null; //返回参数值
    };
    /*****签到日历插件******/
    (function($) {
    var Checkin = function(ele, options) {
        this.ele = ele;
        this.opt = options;
        this.defaults = {
            date:new Date(),
            nowDate:new Date(),
            width: '100%',
            height: 'auto',
            background: 'transparent',
            radius: 10,
            color: '#fff',
            padding: 0,
            opacity:1,
            fontSize:'1rem',
            dateArray: [1, 2,4, 6], // 假设已签到的天数+1
        };
        this.obj = $.extend({}, this.defaults, this.opt);
    };
    Checkin.prototype.init = function() {
        var _self = this.ele,
            html = '',
            myDate = this.obj.date,
            year = myDate.getFullYear(),
            month = myDate.getMonth(),
            day = myDate.getDate(),
            weekText = ['日', '一', '二', '三', '四', '五', '六'],
            /*******记住当前天的日期********/
            nowDate=this.obj.nowDate,
            nowYear = nowDate.getFullYear(),
            nowMonth = nowDate.getMonth(),
            nowDay = nowDate.getDate();
        _self.css({
            fontSize:this.obj.fontSize,
            width: this.obj.width,
            height: this.obj.height,
            background: this.obj.background,
            borderRadius: this.obj.radius,
            color: this.obj.color,
            opacity:this.obj.opacity,
            padding: this.obj.padding
        }).find('.title p').html(year + '年' + (month + 1) + '月' + day + '日');
        $("<ul class='week clearfix'></ul><ul class='calendarList clearfix'></ul>").appendTo(_self);
        for (var i = 0; i < 7; i++) {
            _self.find(".week").append("<li><span>" + weekText[i] + "</span></li>")
        };
        for (var i = 0; i < 42; i++) {
            html += "<li><span></span></li>"
        };
        _self.find(".calendarList").append(html);
        var $li = _self.find(".calendarList").find("li");
        _self.find(".week li").css({
            width: (_self.width() / 7) + 'px',
            height: '3rem',
            boxSizing: 'border-box',
            background: 'transparent'
        });
        $li.css({
            width: (_self.width() / 7) + 'px',
            height: '3rem',
            boxSizing: 'border-box'
        });
//            _self.find(".calendarList").find("li:nth-child(7n)").css('borderRight', 'none');
//            _self.find(".week li:nth-child(7n)").css('borderRight', 'none');
        var monthFirst = new Date(year, month, 1).getDay();
        var d = new Date(year, (month + 1), 0);
        var totalDay = d.getDate(); //获取当前月的天数
        for (var i = 0; i < totalDay; i++) {
            $li.eq(i + monthFirst).find('span').html(i + 1);
            $li.eq(i + monthFirst).addClass('haveData data' +year+month+ (i + 1))
            if (isArray(this.obj.dateArray)) {
                for (var j = 0; j < this.obj.dateArray.length; j++) {
                    if (i == this.obj.dateArray[j]) {
                        // 假设已经签到的
                        $li.eq(i + monthFirst).addClass('checked');
                    }
                }
            }
        }
        //$li.eq(monthFirst+day-1).css('background','#f7ca8e')
        _self.find($(".data" +nowYear+nowMonth+nowDay)).addClass('able-qiandao');
    };
    var isChecked = false;
    Checkin.prototype.events = function() {
        var _self = this.ele;
        var $li = _self.find(".calendarList").find("li");
        // $li.on('click', function(event) {
        //     event.preventDefault();
        //     /* Act on the event */
        //     if ($(this).hasClass('able-qiandao')) {
        //         $(this).addClass('checked');
        //         modal(_self);
        //         isChecked = true;
        //     }
        // });
        // var checkBtn = $(".notChecked");
        // checkBtn.click(function(event) {
        //     modal(_self);
        //     _self.find('.able-qiandao').addClass('checked');
        //     isChecked = true;
        // });
    };
    var modal = function(e) {
        var mask = e.parents().find(".mask");
        var close = e.parents().find(".closeBtn");
        if (mask && !isChecked) {
            mask.addClass('trf');
        } else {
            return
        }
        close.click(function(event) {
            event.preventDefault();
            mask.removeClass('trf')
        });
        e.parents().find('.checkBtn').text("已签到");
    };
    $.fn.Checkin = function(options) {
        var checkin = new Checkin(this, options);
        var obj = [checkin.init(), checkin.events()];
        return obj
    };
    var isArray = function(arg) {
        return Object.prototype.toString.call(arg) === '[object Array]';
    };
})(jQuery);
//生成二维码
})(jQuery);
